﻿Imports System
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO

<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level:=AspNetHostingPermissionLevel.Minimal), DefaultProperty("StartTitle"), ToolboxData("<{0}:ThumbnailSet runat=""server""> </{0}:ThumbnailSet>")>
Public Class ThumbnailSet
    Inherits System.Web.UI.UserControl
    Private thumbFiles As List(Of String) = Nothing
    ' filenames
    Private imgFiles As List(Of String) = Nothing

    <Bindable(True), Category("Appearance"), DefaultValue(""), Description("The first title of the thumbnail set."), Localizable(True)> _
    Public Overridable Property StartTitle() As String
        Get
            Dim s As String = DirectCast(ViewState("StartTitle"), String)
            Return If((s Is Nothing), [String].Empty, s)
        End Get
        Set(value As String)
            ViewState("StartTitle") = value
            StartTitleLabel.Text = value
        End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue(""), Description("The second title of the thumbnail set."), Localizable(True)> _
    Public Overridable Property EndTitle() As String
        Get
            Dim s As String = DirectCast(ViewState("EndTitle"), String)
            Return If((s Is Nothing), [String].Empty, s)
        End Get
        Set(value As String)
            ViewState("EndTitle") = value
            EndTitleLabel.Text = value
        End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue(""), Description("Number of columns in one thumbnail row."), Localizable(True)> _
    Public Overridable Property ColumnsPerRow() As Integer
        Get
            Dim s As Object = DirectCast(ViewState("ColumnsPerRow"), Int32)
            Return If((s Is Nothing), 3, DirectCast(s, Int32))
        End Get
        Set(value As Integer)
            ViewState("ColumnsPerRow") = value
        End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue(""), Description("Spacing of cells in the thumbnail table."), Localizable(True)> _
    Public Overridable Property ThumbCellSpacing() As Integer
        Get
            Dim s As Object = DirectCast(ViewState("ThumbCellSpacing"), Int32)
            Return If((s Is Nothing), ThumbTable.CellSpacing, DirectCast(s, Int32))
        End Get
        Set(value As Integer)
            ViewState("ThumbCellSpacing") = value
            ThumbTable.CellSpacing = value
        End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue(""), Description("Padding of cells in the thumbnail table."), Localizable(True)> _
    Public Overridable Property ThumbCellPadding() As Integer
        Get
            Dim s As Object = DirectCast(ViewState("ThumbCellPadding"), Int32)
            Return If((s Is Nothing), ThumbTable.CellPadding, DirectCast(s, Int32))
        End Get
        Set(value As Integer)
            ViewState("ThumbCellPadding") = value
            ThumbTable.CellPadding = value
        End Set
    End Property

    <Bindable(True), Category("Behavior"), DefaultValue(""), Description("The directory where the thumbnails live."), Localizable(True)> _
    Public Overridable Property ThumbSrc() As String
        Get
            Dim s As String = DirectCast(ViewState("ThumbSrc"), String)
            Return If((s Is Nothing), [String].Empty, s)
        End Get
        Set(value As String)
            ViewState("ThumbSrc") = value
        End Set
    End Property

    <Bindable(True), Category("Behavior"), DefaultValue(""), Description("Wildcard pattern for thumbnail filenames (*.jpg, for example)."), Localizable(True)> _
    Public Overridable Property ThumbFilePattern() As String
        Get
            Dim s As String = DirectCast(ViewState("ThumbFilePattern"), String)
            Return If((s Is Nothing), "*", s)
        End Get
        Set(value As String)
            ViewState("ThumbFilePattern") = value
        End Set
    End Property

    <Bindable(True), Category("Behavior"), DefaultValue(""), Description("The directory where the full-size images live."), Localizable(True)> _
    Public Overridable Property ImgSrc() As String
        Get
            Dim s As String = DirectCast(ViewState("ImgSrc"), String)
            Return If((s Is Nothing), [String].Empty, s)
        End Get
        Set(value As String)
            ViewState("ImgSrc") = value
        End Set
    End Property

    <Bindable(True), Category("Behavior"), DefaultValue(""), Description("Wildcard pattern for full-size image filenames (*.jpg, for example)."), Localizable(True)> _
    Public Overridable Property ImgFilePattern() As String
        Get
            Dim s As String = DirectCast(ViewState("ImgFilePattern"), String)
            Return If((s Is Nothing), "*", s)
        End Get
        Set(value As String)
            ViewState("ImgFilePattern") = value
        End Set
    End Property

    <Bindable(True), Category("Behavior"), DefaultValue(""), Description("Comma-separated list of filenames to exclude from the thumb table."), Localizable(True)> _
    Public Overridable Property ExcludeNames() As String
        Get
            Dim s As String = DirectCast(ViewState("ExcludeNames"), String)
            Return If((s Is Nothing), "*", s)
        End Get
        Set(value As String)
            ViewState("ExcludeNames") = value
        End Set
    End Property

    ' Reads file names from the given directory.
    '
    Protected Sub ReadFileNames(dir As String, pattern As String, toExclude As String, ByRef l As List(Of String))
        Dim di As New DirectoryInfo(Server.MapPath(dir))

        If di.Exists Then
            If l Is Nothing Then
                l = New List(Of String)()
            End If
            Dim names As New List(Of String)(toExclude.Split(","c))

            Dim rgFiles As FileInfo() = di.GetFiles(pattern)

            For Each fi As FileInfo In rgFiles
                If names.IndexOf(fi.Name) = -1 Then
                    ' not found in the exclusion list
                    l.Add(fi.Name)
                End If
            Next
        End If

    End Sub

    Protected Sub Page_PreRender(sender As Object, e As EventArgs)
        ReadFileNames(ThumbSrc, ThumbFilePattern, ExcludeNames, thumbFiles)
        ReadFileNames(ImgSrc, ImgFilePattern, ExcludeNames, imgFiles)

        Dim curColumn As Integer = ColumnsPerRow
        Dim curRow As Integer = -1
        For Each thumb As String In thumbFiles
            If imgFiles.IndexOf(thumb) >= 0 Then
                ' need to make sure the full image for the thumbnail exists
                If curColumn >= ColumnsPerRow Then
                    ' create new row?
                    ThumbTable.Rows.Add(New TableRow())

                    curRow += 1

                    curColumn = 0
                End If

                Dim hl As New HyperLink()
                hl.ImageUrl = ThumbSrc + "/" + thumb
                hl.NavigateUrl = ImgSrc + "/" + thumb


                ThumbTable.Rows(curRow).Cells.Add(New TableCell())
                ThumbTable.Rows(curRow).Cells(curColumn).Controls.Add(hl)
                curColumn += 1
            End If
        Next
    End Sub

    Protected Sub Page_Dispose()
        thumbFiles = Nothing
        imgFiles = Nothing
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

End Class